package com.pp.web.map;

public class MyHashMap {

    Entry [] dataArray = new Entry[2];

    public void put(String key ,String value){
        int hash = key.hashCode();
        Entry saveEntry = new Entry();
        saveEntry.key = key;
        saveEntry.hash = hash;
        saveEntry.value = value;
        //计算下标规则
        int i = hash & (dataArray.length-1);
        //hashcode无冲突
        if(dataArray[i]==null){
            dataArray[i] = saveEntry;
            return;
        }
        Entry entry = dataArray[i];
        do {
            if(entry.key.equals(key)){
                entry.value = value;
               return;
            }
        } while ((entry =entry.next) != null);
        saveEntry.next = dataArray[i];
        dataArray[i] = saveEntry;
    }

    public Object get(String key){
        int hashCode = key.hashCode();
        int index =hashCode & (dataArray.length -1);
        Entry entryTest = dataArray[index];
        if(entryTest==null) return null;
        while (entryTest.hashCode() != key.hashCode()){
            entryTest = entryTest.next;
        }
        return entryTest.value;
    }

}

class T{
    public static void main(String [] args){
        MyHashMap myHashMap = new MyHashMap();
        myHashMap.put("1","1");
        myHashMap.put("1","1.1");
        myHashMap.put("2","2");
        Entry[] dataArray = myHashMap.dataArray;
        for (Entry entry : dataArray){
            System.out.println(entry.value);
        }
    }
}
